package com.vilen.weixin.quartz;

import com.vilen.basic.util.JsonUtil;
import com.vilen.weixin.json.AccessToken;
import com.vilen.weixin.kit.WeixinBasicKit;
import com.vilen.weixin.model.WeixinFinalValue;
import com.vilen.weixin.web.servlet.WeixinContext;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created by Administrator on 2016/2/9.
 */
@Component
public class RefreshAccessTokenTask  {
    public void refreshToken() {
        String url = WeixinFinalValue.ACCESS_TOKEN_URL;
        url = url.replaceAll("APPID", WeixinContext.getInstance().getAppId());
        url = url.replaceAll("APPSECRET",WeixinContext.getInstance().getAppSecret());
        String content = WeixinBasicKit.sendGet(url);
        if(WeixinBasicKit.checkRequestSucc(content)){
            AccessToken at = (AccessToken) JsonUtil.getInstance().json2obj(content,AccessToken.class);
            WeixinContext.getInstance().setAccessToken(at);
        }else {
            refreshToken();
        }

    }
}
